package com.lw.lost.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.lw.lost.databinding.ItemManageReportBinding
import com.lw.lost.models.Report
import java.text.SimpleDateFormat
import java.util.*

class ManageReportAdapter(
    private val onReportClick: (Report) -> Unit,
    private val onActionClick: (Report) -> Unit
) : ListAdapter<Report, ManageReportAdapter.ViewHolder>(DiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            ItemManageReportBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class ViewHolder(
        private val binding: ItemManageReportBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(report: Report) {
            binding.apply {
                tvReporter.text = "举报者：${report.reporterName}"
                tvTarget.text = "被举报者：${report.targetName}"
                tvReason.text = "原因：${report.reason}"
                tvStatus.text = when(report.status) {
                    0 -> "状态：待处理"
                    1 -> "状态：已处理"
                    else -> "状态：未知"
                }
                tvTime.text = "时间：${SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault()).format(Date(report.reportTime))}"

                root.setOnClickListener { onReportClick(report) }
                btnAction.setOnClickListener { onActionClick(report) }
                btnAction.visibility = if (report.status == 0) View.VISIBLE else View.GONE
                btnAction.text = if (report.status == 0) "处理" else "已处理"
            }
        }
    }

    private class DiffCallback : DiffUtil.ItemCallback<Report>() {
        override fun areItemsTheSame(oldItem: Report, newItem: Report) =
            oldItem.reportId == newItem.reportId

        override fun areContentsTheSame(oldItem: Report, newItem: Report) =
            oldItem == newItem
    }
} 